package com.multysite.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Id;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.googlecode.objectify.annotation.Cached;
import com.googlecode.objectify.annotation.Unindexed;

@Cached
public class Tag implements Serializable {
	public static final long serialVersionUID = 1L;
	@Id
	private String alias = "";
	@Unindexed
	private String title = "";
	/*
	 * <alias>news-alias</alias> <title>news Title</title> <content>news
	 * Content</content>
	 */
	@Unindexed
	private String strListNews = "";

	public String getAlias() {
		return alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getStrListNews() {
		return strListNews;
	}

	public void setStrListNews(String strListNews) {
		this.strListNews = strListNews;
	}

	public void tranformListNews(List<News> listNewsObject) {
		try {
			StringBuilder content = new StringBuilder();
			content.append("<list-news>\n");
			if (listNewsObject != null && listNewsObject.size() > 0) {
				for (News news : listNewsObject) {
					content.append("\t<news>\n");
					content.append("\t\t<alias>" + news.getAlias()
							+ "</alias>\n");
					content.append("\t\t<title>" + news.getTitle()
							+ "</title>\n");
					content.append("\t\t<thumb>" + news.getThumb()
							+ "</thumb>\n");
					content.append("\t\t<content>" + news.getSubContent()
							+ "</content>\n");
					content.append("\t</news>\n");
				}
			}
			content.append("</list-news>");
			strListNews = content.toString();
		} catch (Exception e) {
			
		}
	}

	public List<News> recoverListNews() {
		List<News> result = new ArrayList<News>();
		try {
			Document doc = Jsoup.parse(this.getStrListNews());
			Elements tmp;
			tmp = doc.select("news");
			for (Element element : tmp) {
				News obj = new News();
				obj.setAlias(element.select("alias").text());
				obj.setTitle(element.select("title").text());
				obj.setThumb(element.select("thumb").text());
				obj.setContent(element.select("content").text());
				result.add(obj);
			}
		} catch (Exception e) {

		}
		return result;
	}

}
